﻿$(function () {
    VK.Auth.getLoginStatus(function (data) {
        vk_login(data);
    });
});


function vk_login(data) {
    if (data.status != "connected" || getCookie('vk_user') == "undefined" || getCookie('vk_user') == null || getCookie('vk_user') == undefined || getCookie('vk_user') == 'null') {
        $('#vk-login').show();
        $('#vk-user').html('');
    } else {
        $('#vk-login').hide();
        $('#vk-user').show();
        var vkUser = $('#vk-user');
        var user = decodeUserToCookie(JSON.parse(getCookie('vk_user')));

        var vklink = $('<a>').attr({
            href: user.href,
            target: '_blank'
        }).appendTo(vkUser);

        $('<span>').text(user.first_name).appendTo(vklink);
        $('<span>').text(' ').appendTo(vklink);
        $('<span>').text(user.last_name).appendTo(vklink);
        $('<a>').attr({
            href: '#',
            onclick: 'vk_manager.logout(); return false;'
        }).text('Вихід').appendTo(vkUser);


    }
}


function encodeUserToCookie(obj) {
    obj.first_name = encodeURI(obj.first_name);
    obj.last_name = encodeURI(obj.last_name);
    return obj;
}

function decodeUserToCookie(obj) {
    return {
        first_name: decodeURI(obj.first_name),
        last_name: decodeURI(obj.last_name),
        domain: obj.domain,
        href: obj.href,
        id: obj.id,
        nickname: obj.nickname
    };
}


function setCookie(name, value, expires, path, domain, secure) {
    path = '/';
    document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
};

function getCookie(name) {
    var cookie = " " + document.cookie;
    var search = " " + name + "=";
    var setStr = null;
    var offset = 0;
    var end = 0;
    if (cookie.length > 0) {
        offset = cookie.indexOf(search);
        if (offset != -1) {
            offset += search.length;
            end = cookie.indexOf(";", offset);
            if (end == -1) {
                end = cookie.length;
            }
            setStr = unescape(cookie.substring(offset, end));
        }
    }
    return (setStr);
};


function ajaxRequest(url, data, sucessCallBack, errorCallBack) {
    $.ajax({
        url: url,
        type: 'post',
        data: data,
        success: sucessCallBack,
        traditional: true,
        error: errorCallBack
    });
}

function action(actionName, controllerName) {
    return _defaultAction.replace('actionName', actionName).replace('controllerName', controllerName);
}

$(function (e) {
    contentResize();
    //window.onresize = contentResize;
});

var contentResize = function () {
    var _h = $(window).height() - $('.header').outerHeight() - $('.footer').outerHeight() - 5;
    $('.content').css('min-height', _h+'px');
};

function openLogin() {
    $('#login').css({display: 'block'});
}